package com.ruicar.afs.cloud.afscase.autoaudit.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.afscase.autoaudit.entity.ApproveRiskTipsRule;
import com.ruicar.afs.cloud.afscase.autoaudit.entity.LoanRiskTipsRule;
import com.ruicar.afs.cloud.afscase.autoaudit.service.ApproveRiskTipsRuleService;
import com.ruicar.afs.cloud.afscase.autoaudit.service.IRuleAtomDataService;
import com.ruicar.afs.cloud.afscase.autoaudit.service.LoanRiskTipsRuleService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.rules.RuleHelper;
import com.ruicar.afs.cloud.common.rules.constants.RuleRunEnum;
import com.ruicar.afs.cloud.common.rules.dto.RuleResult;
import com.ruicar.afs.cloud.common.rules.dto.RuleRunResult;
import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MultiMap;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: fangchenliang
 * @Date: 2020/7/29 0:56
 */

@Slf4j
@RestController
@RequestMapping("/riskTips")
@AllArgsConstructor
@Api(description = "风险提示接口")
public class RiskTipsController {

    private LoanRiskTipsRuleService loanRiskTipsRuleService;
    private IRuleAtomDataService ruleAtomDataService;
    private ApproveRiskTipsRuleService approveRiskTipsRuleService;
    @PostMapping(value = "/getLoanRiskTips")
    @ApiOperation(value = "放款风险提示信息")
    public IResponse<MultiValueMap> getLoanRiskTips(@RequestParam("contractNo") String contractNo ){
        List<LoanRiskTipsRule> list=new ArrayList();
        Map<Long,List<String>> strMap = new HashMap<>();
        try{
            JSONObject jsonObject=ruleAtomDataService.getCommonAtomRunParamsLoan(contractNo);
            log.info("合同号码{}风险提示规则报文：{}",contractNo,jsonObject);
            RuleRunResult ruleRunResult = RuleHelper.runByGroup(jsonObject, "loanRisk", false, RuleRunEnum.PARALLEL, false);
            boolean isHit = ruleRunResult.getHit();
            log.info("放款风险提示规则组：{}=>执行结束，结果：{}", "loanRisk", isHit);
            if (isHit) {
                List<RuleResult> ruleResults = ruleRunResult.getResults();
                ruleResults.forEach(ruleResult -> {
                    String ruleNo = ruleResult.getRuleNo();
                    LoanRiskTipsRule loanRiskTipsRule = loanRiskTipsRuleService.getById(Long.valueOf(ruleNo));
                    list.add(loanRiskTipsRule);
                });
                strMap =  list.stream().collect(Collectors.groupingBy(LoanRiskTipsRule::getAttachmentId,Collectors.mapping(LoanRiskTipsRule::getRemarks, Collectors.toList())));
            }
        }catch (Exception e){
            log.info("放款风险提示规则组，{}:执行失败", "loanRisk",e);
            return IResponse.success(new ArrayList<>());
        }
        return IResponse.success(strMap);
    }

    @PostMapping(value = "/getCreditRiskTips")
    @ApiOperation(value = "信审风险提示信息")
    public IResponse<List<String>> getCreditRiskTips(@RequestParam("applyNo") String applyNo ){
        List<String> list=new ArrayList();
        try{
            JSONObject jsonObject=ruleAtomDataService.getCommonAtomRunParams(applyNo);
            log.info("申请编号{}风险提示规则报文：{}",applyNo,jsonObject);
            RuleRunResult ruleRunResult = RuleHelper.runByGroup(jsonObject, "approveRisk", false, RuleRunEnum.PARALLEL, false);
            boolean isHit = ruleRunResult.getHit();
            log.info("信审风险提示规则组：{}=>执行结束，结果：{}", "approveRisk", isHit);
            if (isHit) {
                List<RuleResult> ruleResults = ruleRunResult.getResults();
                ruleResults.forEach(ruleResult -> {
                    String ruleNo = ruleResult.getRuleNo();
                    ApproveRiskTipsRule approveRiskTipsRule = approveRiskTipsRuleService.getById(Long.valueOf(ruleNo));
                    list.add(approveRiskTipsRule.getRemarks());
                });
            }
        }catch (Exception e){
            log.info("信审风险提示规则组，{}:执行失败", "approveRisk",e);
            return IResponse.success(new ArrayList<>());
        }
        return IResponse.success(list);
    }
}
